*************************************************************
* Replication codes for analyses in Section 4 (on soybean) of
* Beyond Yield Response: Weather Shocks and Crop Abandonment
* May 2020
*************************************************************

	clear
	gen initial = .
	
	save dta\boot_bote_soy, replace
	
	set seed 123456
	
	forvalues t = 1/1000 {
	
	use dta\bote_population_pcws, clear
	
	qui keep if prod_soy!=.

	forvalues t= 5(5)20 {
		forvalues k=8(3)38 {
			qui gen d`k'_`t' = rgs_dd3bin`k'_bote`t' - rgs_dd3bin`k'_bote0
			}
		}
	
	bsample 1415
	
* bote calculations

	* spline reg
	forvalues t = 5(5)20 {
	
	qui gen spline_soy_loss_ha_`t' = 0.0047 *(gdd8_26_bote`t' - gdd8_26_bote0) + ///
							-0.0002 *(gdd26_35_bote`t' - gdd26_35_bote0) + ///
							-0.0050 *(dday35C_bote`t' - dday35C_bote0)
	
	qui gen spline_soy_loss_yld_`t' =  0.0699 *(gdd8_26_bote`t' - gdd8_26_bote0) + ///
							-0.0034 *(dday26C_bote`t' - dday26C_bote0) 
	
	qui replace spline_soy_loss_ha_`t' = 1 - acres_soy_ratio_harvest if spline_soy_loss_ha_`t' + acres_soy_ratio_harvest > 1
	qui replace spline_soy_loss_yld_`t' = -1 if spline_soy_loss_yld_`t'<-1
	}

	forvalues t = 5(5)20 {
	qui gen spline_soy_ttloss_yld`t' = (spline_soy_loss_yld_`t')*pl_acre_soy
	qui gen spline_soy_ttloss_ha`t' = (spline_soy_loss_ha_`t')*pl_acre_soy
	}
	
	* tAvg reg
	forvalues t = 5(5)20 {
	qui gen tAvg_soy_loss_ha_`t' = 0.0170*(tAvg_S1a_bote`t' - tAvg_S1a_bote0) -0.0006*(tAvg_S1a_bote`t'^2 - tAvg_S1a_bote0^2) + ///
							 0.1026*(tAvg_S2a_bote`t' - tAvg_S2a_bote0) -0.0029*(tAvg_S2a_bote`t'^2 - tAvg_S2a_bote0^2)
	
	qui gen tAvg_soy_loss_yld_`t' =  0.0371*(tAvg_S1a_bote`t' - tAvg_S1a_bote0) -0.0004*(tAvg_S1a_bote`t'^2 - tAvg_S1a_bote0^2) + ///
							 0.5604*(tAvg_S2a_bote`t' - tAvg_S2a_bote0) -0.0162*(tAvg_S2a_bote`t'^2 - tAvg_S2a_bote0^2)	
							 
	qui replace tAvg_soy_loss_ha_`t' = 1 - acres_soy_ratio_harvest if tAvg_soy_loss_ha_`t' + acres_soy_ratio_harvest > 1
	qui replace tAvg_soy_loss_yld_`t' = -1 if tAvg_soy_loss_yld_`t'<-1
	}

	forvalues t = 5(5)20 {
	qui gen tAvg_soy_ttloss_yld`t' = (tAvg_soy_loss_yld_`t')*pl_acre_soy
	qui gen tAvg_soy_ttloss_ha`t' = (tAvg_soy_loss_ha_`t')*pl_acre_soy
	}
	
	* bin reg
	forvalues t = 5(5)20 {
	qui gen bin_soy_loss_ha_`t' = .0615*d8_`t' +.2536*d11_`t' +.0567*d14_`t' +.0200*d17_`t' +.0630*d20_`t' + ///
							.4590*d23_`t' + .1615*d26_`t' +.0014*d29_`t' -.1835*d32_`t' -1.0333*d35_`t' -6.4515*d38_`t'
	
	qui gen bin_soy_loss_yld_`t' =  -.3194*d8_`t' -.1783*d11_`t' -.5275*d14_`t' +.3839*d17_`t' +1.2368*d20_`t' + ///
							2.4483*d23_`t' + .9258*d26_`t' -.8707*d29_`t' -3.8622*d32_`t' -6.7608*d35_`t' -9.2288*d38_`t'

	qui replace bin_soy_loss_ha_`t' = 1 - acres_soy_ratio_harvest if bin_soy_loss_ha_`t' + acres_soy_ratio_harvest > 1
	qui replace bin_soy_loss_yld_`t' = -1 if bin_soy_loss_yld_`t'<-1
	}

	forvalues t = 5(5)20 {
	qui gen bin_soy_ttloss_yld`t' = (bin_soy_loss_yld_`t')*pl_acre_soy
	qui gen bin_soy_ttloss_ha`t' = (bin_soy_loss_ha_`t')*pl_acre_soy
	}							
	
	qui collapse (sum) *ttloss* pl_acre*
	
	foreach x in spline tAvg bin {
	forvalues t = 5(5)20 {
	qui gen `x'_soy_shrloss_yld`t' = `x'_soy_ttloss_yld`t'/pl_acre_soy
	qui gen `x'_soy_shrloss_ha`t' = `x'_soy_ttloss_ha`t'/pl_acre_soy
	gen `x'_soy_loss_overest`t' = 1-(`x'_soy_shrloss_ha`t'+`x'_soy_shrloss_yld`t')/`x'_soy_shrloss_yld`t'
	}
	}
		
	keep *soy_shr* *soy_loss*
	
	save dta\soy_boot_temp, replace
	use dta\boot_bote_soy, clear
	append using dta\soy_boot_temp
	
	save dta\boot_bote_soy, replace
	}

	use dta\boot_bote_soy, clear
	
	sum *soy_shrloss_ha* 
	sum *soy_shrloss_yld* 
	
	sum *soy_loss*
	
	